package com.hex.es.aop;

import lombok.Setter;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.lang.reflect.Proxy;

/**
 * @ClassName EsFactoryBean
 * @Description EsFactoryBean
 * @Author hex
 * @Date 2020/12/18 18:14
 * @Version 1.0
 */
@Setter
public class EsFactoryBean implements FactoryBean<Object>, ApplicationContextAware {

    private Class classInterface;

    private ApplicationContext applicationContext;

    @Override
    public boolean isSingleton() {
        return true;
    }

    @Override
    public Object getObject() throws Exception {
        EsInvocationHandler esInvocationHandler = new EsInvocationHandler(applicationContext);
        return Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class<?>[]{classInterface}, esInvocationHandler);
    }

    @Override
    public Class<?> getObjectType() {
        return classInterface;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
